package com.taotao.portal.service.ipml;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.pojo.TbUser;
import com.taotao.portal.service.IUserService;

/**
 * 用户
 * 
 * @author Administrator
 *
 */
@Service
public class UserServiceIpml implements IUserService {

	@Value("${SSO_BASE_URL}")
	public String SSO_BASE_URL;

	@Value("${SSO_USER_TOKEN}")
	private String SSO_USER_TOKEN;

	@Value("${SSO_PAGE_LOGIN}")
	public String SSO_PAGE_LOGIN;

	@Value("${SSO_PAGE_LOGOUT}")
	public String SSO_PAGE_LOGOUT;

	/**
	 * 通过token获取用户信息
	 */
	@Override
	public TbUser getUserByToken(String token) {
		// TODO Auto-generated method stub
		if (StringUtils.isNoneBlank(token)) {
			String doGet = HttpClientUtil.doGet(SSO_BASE_URL + SSO_USER_TOKEN + token);
			if (StringUtils.isNotBlank(doGet)) {
				TaotaoResult taotaoResult = TaotaoResult.formatToPojo(doGet, TbUser.class);
				return (TbUser) taotaoResult.getData();
			}
		}
		return null;
	}

	/**
	 * 用户退出
	 */
	@Override
	public TaotaoResult logout(String token) {
		// TODO Auto-generated method stub
		String doGet = HttpClientUtil.doGet(SSO_BASE_URL + SSO_PAGE_LOGOUT + token);
		if (StringUtils.isNotBlank(doGet)) {
			TaotaoResult result = TaotaoResult.format(doGet);
			if (200 == result.getStatus()) {
				return result;
			}
		}
		return TaotaoResult.build(400, "退出失败");
	}
}
